/**
 * 	create by xudongzhang at 2023-02-14
 */
const { execSync } = require ("child_process");
const dateFormat =  require('./dateformat')

// 获取当前git分支信息
function getBranchVersionInfo() {
  // 当前分支名
  const branchName = execSync("git name-rev --name-only HEAD").toString().trim();
  // 提交的commit hash
  const lastHash = execSync("git show -s --format=%H").toString().trim();
  // 提交人姓名
  const commitUser = execSync("git show -s --format=%cn").toString().trim();
  // 提交日期
  const c_date = new Date(execSync("git show -s --format=%cd").toString())
  const commitDate = dateFormat(c_date);
  // 提交描述
  const commitMsg = execSync("git show -s --format=%s").toString().trim();
  // 最近提交5次commit
  const last5Msg = execSync("git log -5 --format=%s").toString().trim();
  const buildDate = dateFormat(new Date())
  return {
    branchName,
    lastHash,
    commitUser,
    commitDate,
    commitMsg,
    last5Msg,
    buildDate
  };
}

module.exports = getBranchVersionInfo